<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Games</title>
</head>

<link rel="stylesheet" type="text/css" href="../css/master.css" media="screen" />
<body>

<script type="text/javascript">
function viewDetails(cc)
{
	
	
	document.getElementById("gameid").value = cc;
	document.getElementById("showvote").value = "showvote";
	
	document.forms['glf'].action="gameDetails.php";
	document.forms['glf'].submit();
	
	
}



</script>	

	
<?php include("../db.php");

session_start();
check_inactive();
authenticate_player(); 


$db = pg_connect('host=dbsrv1 dbname=csc309g7 user=csc309g7 password=aiboid4p');


if($db)
{

$myuserid = $_SESSION['userid'];


	
?>


	
	
<fieldset>
	<legend>Past Game</legend> 
	
	<form name="glf" method="post">
		<input type="hidden" id="gameid" name="gameid" />
		<input type="hidden" id="showvote" name="showvote" />
		
	<?php
	 $returned_string = draw_sort_bar_in_game($_POST['sort_by_select'], $_POST['search_by_select'], $_POST['search_term']);	 
	 
	 $sort_by = substr($returned_string, 0 , strrpos($returned_string, ";"));
	 $and_like = substr($returned_string, strrpos($returned_string, ";")+1);
	 
	 
	 //echo "$sort_by and $and_like";
	  
	 
	?>
	
	<div class="content events">
			
					<dl>						
						<?php 
						
							//no need
						
						
						
						if($_GET['location_id'])
						{
							$location_id = $_GET['location_id'];
							
							
							$query = "select * from (select gameid, sport_name, username, creator, start_date, start_time, location_name, title, location from 
								(select * from (select * from games g left join location l on g.location = l.lid where g.location = ".$location_id.") a left join sports s on a.sid = s.sid) b
								left join people p on b.creator = p.userid) x left join usergame ug on x.gameid = ug.gameid where  ug.userid = $myuserid and start_date < now() $and_like order by $sort_by ";
							
						}
						else
						{
							$query = "select * from (select gameid, sport_name, username, creator, start_date, start_time, location_name, title, location from 
								(select * from (select * from games g left join location l on g.location = l.lid) a left join sports s on a.sid = s.sid) b 
								left join people p on b.creator = p.userid) x left join usergame ug on x.gameid = ug.gameid where  ug.userid = $myuserid and start_date < now() $and_like order by $sort_by";
						}
						
						
						
						
						$result = pg_query($query);
						if (pg_num_rows($result)!=0)
						{
							$ind=1;
	
							while($current_games = pg_fetch_assoc($result))
							{
								
								
								if($ind%2==0)
									$background = "";
								else 
									$background = "alt";
								
							
								echo "<dt class='".$background."'><strong>" . date("l", strtotime($current_games['start_date'])) . "  </strong>" . date("M d", strtotime($current_games['start_date']));
								
								
								
									
								echo "<a  class='btn save'  ><img src='../img/done.gif' alt='Join this game' width='28' height='16' border='0'></a></dt>";
										
									
								
								echo "<dd class='$background'>  
									  <h4>" .  $current_games['sport_name']  . ": <a class='event_title' href=\"javascript:viewDetails('".$current_games['gameid']."');\" >" .  $current_games['title'] ."</a> 									
									  " . $current_games['start_time'] . "						    
										@&nbsp;<a class='locale' href='?location_id=".$current_games['location']."'>" . $current_games['location_name'] ."</a>					              
									</h4>
									<p class='attendees'> Created by <a href='../profile_player.php?pagesource=pastGame&viewuserid=".$current_games['creator']."' />" . $current_games['username'] . "</a> </p>
							</dd>";
								
							$ind++;
							}
							
								?>
								
					</dl>
					<?php 
										
					
							
						}
						else//return 0 rows
						{
							
							echo "<p>You don't have any past games yet, or your search doesn't match any records.</p>";
							
						}
					
					
						
					?>
						
						
		</div>
		
		
		</form>
		
</fieldset>

	
<?php
	
	
	
	
	





}
else
{
	echo "no connection made";
}



?>
</body>
</html>
